package top.littlejiang.onlinexam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.littlejiang.onlinexam.common.base.BasePage;
import top.littlejiang.onlinexam.common.base.BaseServiceImpl;
import top.littlejiang.onlinexam.common.utils.HttpContextUtils;
import top.littlejiang.onlinexam.common.utils.IPUtils;
import top.littlejiang.onlinexam.common.utils.JWTUtils;
import top.littlejiang.onlinexam.entity.OlexamProctorLog;
import top.littlejiang.onlinexam.mapper.OlexamProctorLogMapper;
import top.littlejiang.onlinexam.service.OlexamProctorLogService;

/**
 * <p>
 * 监考日志 服务实现类
 * </p>
 *
 * @author chen
 * @since 2021-03-11
 */
@Service
public class OlexamProctorLogServiceImpl extends BaseServiceImpl<OlexamProctorLogMapper, OlexamProctorLog> implements OlexamProctorLogService {

    @Autowired
    OlexamProctorLogMapper logMapper;

    /**
     * 监考日志分页查询
     *
     * @param page
     * @return
     */
    @Override
    public IPage<OlexamProctorLog> getlogListPage(BasePage<OlexamProctorLog> page) {
        OlexamProctorLog queryParam = page.getQueryParam();
        QueryWrapper<OlexamProctorLog> wrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(queryParam.getFkUserName())){
            wrapper.like("fk_user_name", queryParam.getFkUserName());
        }
        if (!StringUtils.isBlank(queryParam.getFkExamName())){
            wrapper.like("fk_exam_name", queryParam.getFkExamName());
        }
        if (page.getPageSize() <= 0){
            page.setPageSize(10);
        }
        Page<OlexamProctorLog> p = new Page<>(page.getPageIndex(), page.getPageSize());
        return this.baseMapper.getlogListPage(p, wrapper);
    }

    /**
     * 保存一条记录
     *
     * @param log
     */
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void insertOne(OlexamProctorLog log) {
        log.setFkUserId(JWTUtils.getUserId())
                .setFkUserName(JWTUtils.getUserName())
                .setIpAddress(IPUtils.getIpAddr(HttpContextUtils.getHttpServletRequest()))
                .setIsDel(0);
        logMapper.insert(log);
    }
}
